package cn.b.sky.mes.pp.controller;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import cn.b.sky.mes.pp.model.ProductionOrderPlan;
import cn.b.sky.mes.pp.model.condition.ProductionOrderPlanCondition;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.b.sky.mes.cm.service.WorkCenterService;
import cn.b.sky.common.file.ExcelUtil;
import cn.b.sky.common.utils.DateUtils;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.mes.cm.enums.ECodeEnums;
import cn.b.sky.user.service.ECodeService;
import cn.b.sky.user.service.model.ECode;
import cn.b.sky.mes.pp.service.OrderTypeService;
import cn.b.sky.mes.pp.service.ProductionOrderPlanService;

/**
 * 
 * <strong>Title : ProductionOrderPlanController.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.pp.controller<br>
 * </strong> <strong>Description : </strong>生产订单计划相关控制器<br>
 * <strong>Create on : 2016-5-24 上午9:58:28<br>
 * </strong>
 * <p>
 * 
 * @author JunLin.Yang<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人|修改日期|修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "/productionOrderPlan/*")
@Slf4j
public class ProductionOrderPlanController extends SecurityController {

  @Autowired
  ProductionOrderPlanService productionOrderPlanService;

  @Autowired
  ECodeService eCodeService;

  @Autowired
  OrderTypeService orderTypeService;

  @Autowired
  WorkCenterService workCenterService;

  /**
   * 
   * <strong>Title : listProductionOrderPlansWithExtInfo<br>
   * </strong> <strong>Description : </strong>查询订单计划信息（分页）<br>
   * <strong>Create on : 2016-5-25 上午9:55:07<br>
   * </strong>
   * <p>
   * 
   * @param navTabId
   *            页面TabId
   * @param condition
   *            订单计划查询对象
   * @param model
   *            Model属性
   * @return 页面路径地址 String
   * @throws
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "listProductionOrderPlansWithExtInfo")
  public String listProductionOrderPlansWithExtInfo(String navTabId,
                                                    @ModelAttribute("condition") ProductionOrderPlanCondition condition, Model model) {
    String languageCode = this.getCurrentLanguage();
    String siteCode = this.getSiteCode();

    model.addAttribute(NAVTABID, navTabId);

    // 添加SITECODE条件值并查询分页信息
    condition.setSiteCode(siteCode);
    model.addAttribute("page",
        this.productionOrderPlanService.selectProductionOrderPlansWithExtInfoPageByCondition(condition));

    // 订单类型信息集合
    model.addAttribute("orderTypeList", this.orderTypeService.getAllOrderTypes(siteCode));

    // 工作中心信息集合
    model.addAttribute("workCenterList", workCenterService.getAllWorkCenterListBySiteCode(siteCode, 0));

    // 订单类型信息国际化编码信息集合
    model.addAttribute("productionOrderTypeEcodeMap",
        this.eCodeService.selectECodeMap(ECodeEnums.PRODUCTIONORDER_TYPE.getCode(), languageCode));

    return "/pp/productionOrderPlan/productionOrderPlan_list";
  }

  /**
   * 
   * <strong>Title : viewProductionOrderPlanWithExtInfo<br></strong>
   * <strong>Description : </strong>查询订单计划信息详情<br> 
   * <strong>Create on : 2016-6-1 上午9:08:28<br></strong>
   * <p>
   * @param navTabId 页面TabId
   * @param id 订单计划信息ID
   * @param model Model属性
   * @return 页面路径地址
   * String 
   * @throws 
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "viewProductionOrderPlanWithExtInfo")
  public String viewProductionOrderPlanWithExtInfo(String navTabId, String id, Model model) {
    String languageCode = this.getCurrentLanguage();
    String siteCode = this.getSiteCode();

    model.addAttribute(NAVTABID, navTabId);

    // 订单计划详情
    model.addAttribute("productionOrderPlan",
        this.productionOrderPlanService.getProductionOrderPlanWithExtInfoByPK(siteCode, id));

    // 订单类型信息国际化编码信息集合
    model.addAttribute("productionOrderTypeEcodeMap",
        this.eCodeService.selectECodeMap(ECodeEnums.PRODUCTIONORDER_TYPE.getCode(), languageCode));

    return "/pp/productionOrderPlan/productionOrderPlan_view";
  }

  /**
   * 
   * <strong>Title : exportProductionOrderPlansWithExtInfoToExcel<br>
   * </strong> <strong>Description : </strong>以Excel方式导出订单计划信息<br>
   * <strong>Create on : 2016-5-25 上午9:56:08<br>
   * </strong>
   * <p>
   * 
   * @param response
   *            Http响应对象
   * @param navTabId
   *            页面TabId值
   * @param condition
   *            订单计划查询对象 void
   * @throws
   * @author JunLin.Yang<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "exportProductionOrderPlansWithExtInfoToExcel")
  public void exportProductionOrderPlansWithExtInfoToExcel(HttpServletResponse response, String navTabId,
      ProductionOrderPlanCondition condition) {
    Map<String, String> map = new LinkedHashMap<String, String>();
    String languageCode = SecurityContextUtil.getLanguageCode();

    map.put("workCenterText", getImeViewMessageI18n(languageCode, "workCenter.C"));
    map.put("statuseText", getImeViewMessageI18n(languageCode, "orderStatusECode.C"));
    map.put("productionOrderNum", getImeViewMessageI18n(languageCode, "productionOrderNum.C"));
    map.put("materialCode", getImeViewMessageI18n(languageCode, "materialCode.C"));
    map.put("materialText", getImeViewMessageI18n(languageCode, "materialText.C"));
    map.put("plannedQuantity", getImeViewMessageI18n(languageCode, "plannedQuantity.C"));
    map.put("plannedMinutesStr", getImeViewMessageI18n(languageCode, "plannedTime.C") + "("
        + getImeViewMessageI18n(languageCode, "minute.C") + ")");
    map.put("plannedStartDateStr", getImeViewMessageI18n(languageCode, "plannedDate.C"));
    map.put("requirementDateStr", getImeViewMessageI18n(languageCode, "requirementDate.C"));
    map.put("productionLotNum", getImeViewMessageI18n(languageCode, "productionlotNum.C"));
    map.put("plannedstartDateTime", getImeViewMessageI18n(languageCode, "plannedstartDateTime.C"));
    map.put("plannedendDateTime", getImeViewMessageI18n(languageCode, "plannedendDateTime.C"));

    // 订单类型信息国际化编码信息集合
    Map<String, ECode> productionOrderTypeEcodeMap = this.eCodeService
        .selectECodeMap(ECodeEnums.PRODUCTIONORDER_TYPE.getCode(), languageCode);

    // 添加SITECODE条件值
    condition.setSiteCode(this.getSiteCode());
    List<ProductionOrderPlan> dataList = this.productionOrderPlanService
        .selectProductionOrderPlansWithExtInfoByCondition(condition);
    ECode ecode = null;

    if (null == dataList) {
      // 如果数据不存在，则赋默认值
      dataList = new ArrayList<ProductionOrderPlan>(0);
    }

    // 循环赋值订单状态信息及更新日期状态
    DateUtils dateUtils = DateUtils.getDateUtils(DateUtils.YYYY_MM_DD);
    for (ProductionOrderPlan productionOrderPlan : dataList) {
      ecode = productionOrderTypeEcodeMap.get(productionOrderPlan.getStatuseCode());
      if (null != ecode) {
        productionOrderPlan.setStatuseText(ecode.geteCodeText());
      }

      if (null != productionOrderPlan.getRequirementDate()) {
        productionOrderPlan.setRequirementDateStr(dateUtils.format(productionOrderPlan.getRequirementDate()));
      }

      if (null != productionOrderPlan.getPlannedStartDate()) {
        productionOrderPlan.setPlannedStartDateStr(dateUtils.format(productionOrderPlan.getPlannedStartDate()));
      }

    }

    try {
      ExcelUtil.exportToExcel(response, getImeViewMessageI18n(languageCode, "productionOrderPlanReport.C"), map,
          dataList);
    } catch (Exception e) {
      log.error("{}", e);
    }
  }

}
